package regexp.homework;

import regexp.utils.RegExpFind;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author DCSGO
 * @version 1.0
 * 3.对一个url进行解析
 * http://www.sohu.com:8080/abc/index.htm
 * a)要求得到协议是什么? http
 * b)域名是什么? www.sohu.com
 * c)端口是什么? 8080
 * d)文件名是什么?    index.htm
 */
public class Homework3 {
    public static String[] analysis1(String url) {
        String[] strings = url.split("://|/|:");
        return new String[]{strings[0], strings[1], strings[2], strings[strings.length - 1]};
    }

    public static String[] analysis2(String url) {
        Pattern pattern = Pattern.compile("^([a-zA-Z]+)://([a-zA-Z.]+):(\\d+)[\\w-/]*/([\\w.-]+)$");
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            String[] ans = new String[4];
            ans[0] = matcher.group(1);
            ans[1] = matcher.group(2);
            ans[2] = matcher.group(3);
            ans[3] = matcher.group(4);
            return ans;
        } else {
            return null;
        }
    }

    public static void main(String[] args) {
        String[] analysis = analysis2("http://www.baidu.ai.com:8080/abc/123/baid/index.htm");
        if (analysis != null) {
            System.out.println(Arrays.toString(analysis));
        } else {
            System.out.println("不规范的url!");
        }
        //[http, www.baidu.ai.com, 8080, index.htm]
    }
}
